package com.yehuishou.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.LunBo;
import com.yehuishou.service.LunBoService;

/**
 * 轮播图管理
 * @author VULCAN
 *
 */
@Controller
public class LunBoController {
	
	@Autowired
	private LunBoService lunBoService;
		
	@RequestMapping("/content-6_2")
	public String showLunBo(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="7") Integer pageSize,HttpServletRequest request,LunBo lunBo) {
		
		PageInfo<LunBo> lb =lunBoService.LunBoFindAll(pageNum,pageSize,lunBo);
		
		request.setAttribute("lb", lb);
		
		return "content-6_2";
	}
	
	@RequestMapping("/goAddLunBo")
	public String goAddLunBo() {
		
		return "content-6_2.1";
	}
	
	@RequestMapping("/addLunBo")	
	public String AddLunBo (LunBo lunBo) {
		
		Integer lb =lunBoService.InsertLunBo(lunBo);
		if(lb!=null) {
			return "redirect:content-6_2";
		}
		return null;
		
	}
	
	@ResponseBody
	@RequestMapping("/deleteLunBo")
	public Integer deleteLunBo (Integer lbid) {
		
		Integer lb =lunBoService.deleteLunBo(lbid);
		
		return lb;
	}
	
	@RequestMapping("/toupdate")
	public ModelAndView toupdate(Integer lbid) {
		LunBo lb =lunBoService.findLunBo(lbid);
		ModelAndView mv =new ModelAndView("content-6_2.2");
		mv.addObject("lb",lb);
		return mv;
	}
	
	
	@RequestMapping("/updateLunBo")
	public String updateLunBo(LunBo lunbo) {
		
		Integer lb = lunBoService.updateLunBo(lunbo);
		if(lb!=null) {
			return "redirect:content-6_2";
		}
		return null;
	}
	
	// 商品图片修改
	/*@ResponseBody
	@RequestMapping("imgupdate")
	public Map<String, Object> imgupdate(@RequestParam(defaultValue = "public") String model, @RequestParam String ext,
			@RequestParam MultipartFile file) {
		Map<String, Object> map = new HashMap<>();
		String rootPath = "/upload/";
		// 组装文件存储路径
		String filePath = rootPath + model + File.separator;

		// 创建目录
		File f = new File(filePath);
		if (!f.exists()) {
			f.mkdirs();
		}

		// 2.生成新的文件名
		String newFileName = UUID.randomUUID().toString() + ext;

		try {
			// 把上传的文件存储指定位置
			file.transferTo(new File(f, newFileName));
			// 文件上传操作
			String filepath = rootPath + model + File.separator + newFileName;
			String path = filepath.replace("\\", "/");
			ImageFile imgfile = new ImageFile();
			imgfile.setFilePath(path);
			int i = sellitemservice.save(imgfile);
			if (i > 0) {
				int id = imgfile.getFileId();
				String imgid = ";" + Integer.toString(id);
				map.put("imgid", imgid);
				map.put("filepath", filepath);
				return map;
			}
			return map;
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return map;

	}*/
	
}
